package com.duan.luren.service;

import com.duan.luren.common.BusinessExceptionUtils;
import com.duan.luren.config.RabbitMQConfiguration;
import com.duan.luren.mq.consumer.AbstractIdempotentConsumer;
import com.duan.luren.mq.dto.Msg;
import com.duan.luren.po.OrderPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 订单消息消费者
 *
 */
@Slf4j
@Component
public class OrderConsumer extends AbstractIdempotentConsumer<OrderPO, Msg<OrderPO>> {

    @RabbitListener(queues = RabbitMQConfiguration.Order.QUEUE)
    public void dispose(Message message) {
        super.dispose(message);
    }

    @Override
    protected void disposeIn(Message message, OrderPO body) {
        try {
            log.info("***************开始消费，收到订单消息：{}", body);
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        //这里是为了演示消费异常重试的场景，这里加了个判断，金额为空的时候，弹出异常
        if (body.getPrice() == null) {
            throw BusinessExceptionUtils.businessException("订单金额有误啊!");
        }
    }
}
